#!/bin/bash

ports="6379"
if [ ! -z $PORTS ]
then
    ports=$PORTS
fi
ports=${ports//,/ }

buildRoot=/build
pkgRoot=$buildRoot/pkg
dataDir=/data/redis

cd $dataDir
for p in $ports
do
    if [ ! -d $p ]
    then
        mkdir $p
        cd $p
        mkdir data logs
        cp -r $pkgRoot/conf ./
        sed -i "s/6379/$p/g" conf/redis.conf
        cd ../
    fi

    /usr/local/redis/bin/redis-server /data/redis/$p/conf/redis.conf &
done

rports="6380,6381,6382"
if [ ! -z $RPORTS ]
then
    rports=$RPORTS
fi
rports=${rports//,/ }

hpList=""
for p in $rports
do
    if [ ! -d $p ]
    then
        mkdir $p
        cd $p
        mkdir data logs
        cp -r $pkgRoot/conf ./
        sed -i "s/6379/$p/g" conf/redis.conf
        sed -i "s/requirepass/#requirepass/g" conf/redis.conf
        sed -i "s/#cluster/cluster/g" conf/redis.conf
        cd ../
    fi

    /usr/local/redis/bin/redis-server /data/redis/$p/conf/redis.conf &
    hpList="$hpList 127.0.0.1:$p"
done

source $HOME/.bashrc
echo yes | redis-trib.rb create $hpList

sleep infinity
